package com.lalosoft.misgastos.config;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESEncrypt {

	private final String ALGORITHM = "AES";
//	private final int KEY_LENGHT = 256;
	private final String ENCODING = "UTF-8";
	private final String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5Padding";
	private byte[] parameterSpecification  = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; //16 random bytes
	
	private String key;
//	private Context context;
//	private static AESEncrypt instance;
//	
//	public static AESEncrypt getInstance(Context context) throws Exception {
//		if(instance == null){
//			instance = new AESEncrypt(context);
//		}
//		return instance;
//	}
//	
//	private AESEncrypt(Context context) throws Exception{
//		this.context = context;
//		init();
//	}
//	
//	private void init() throws Exception{
//		if(!GastosManager.getInstance(context).isStarted()){
//			generateKey();
//			GastosManager.getInstance(context).started();
//		}
//		retrieveKey();
//	}
	
//	private void retrieveKey() throws IOException {
//		key = GastosManager.getInstance(context).getAesKey();
//	}
	
//	public void generateKey() throws Exception {
//		KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
//		kgen.init(KEY_LENGHT);
//		SecretKey skey = kgen.generateKey(); 
//		key = HexToString(skey.getEncoded());
//		GastosManager.getInstance(context).setAesKey(key);
//	}

	public String encrypt(String text) throws Exception	{  

		  byte[] raw = StringToHex(key);
		  SecretKeySpec skeySpec = new SecretKeySpec(raw, ALGORITHM);
		  Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
		  IvParameterSpec ivspec = new IvParameterSpec(parameterSpecification);
		  cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec);
		  byte[] encrypted = cipher.doFinal(text.getBytes(ENCODING));
		  String encryptedText = HexToString(encrypted);
		  return encryptedText;
	}
	
	private String HexToString(byte[] encryptedArray) {
		String encryptedText = "";
		for (int i = 0; i < encryptedArray.length; i++) {
			int aux = encryptedArray[i] & 0xff;
			if (aux < 16) {
				encryptedText = encryptedText.concat("0");
			}
			encryptedText = encryptedText.concat(Integer.toHexString(aux));
		}
		return encryptedText;
	}

	private byte[] StringToHex(String encrypted) {
		byte[] enBytes = new byte[encrypted.length() / 2];
		for (int i = 0; i < enBytes.length; i++) {
			int index = i * 2;
			String aux = encrypted.substring(index, index + 2);
			int v = Integer.parseInt(aux, 16);
			enBytes[i] = (byte) v;
		}
		return enBytes;
	}
	
	public String decrypt(String encrypted) throws Exception {
         IvParameterSpec ivspec = new IvParameterSpec(parameterSpecification);		
		 byte[] raw = StringToHex(key);
		 SecretKeySpec skeySpec = new SecretKeySpec(raw, ALGORITHM);
		 Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);  
		 cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec);
		 byte[] original = cipher.doFinal(StringToHex(encrypted));
		 String originalString = new String(original);
		 return originalString;
	}
	
	public void setKey(String key){
		this.key = key;
	}

	public String getKey(){
		return key;
	}
}
